import {isNull} from '@/utils'
import appConfig from '../app.config'
import Taro from '@tarojs/taro'

// console.log('pages', appConfig)

const router = {
  to: undefined,
  from: undefined,
  param: {},
  query: {},
  pages: [],
  beforeEachFun: undefined,
  beforeEach: function (fun) {
    this.beforeEachFun = fun
  },
  afterEachFun: undefined,
  afterEach: function (fun) {
    this.afterEachFun = fun
  },
  init: function() {
    appConfig.pages.forEach(item => {
      const page = {}
      if (item instanceof Object){
        page.path = item.path
      } else if (typeof item === 'string') {
        page.path = item
      }
      this.pages.push(page)
    })
    // console.log('init', this.pages)
  },
  reset: function () {
    router.to = undefined
    router.param = {}
    router.query = {}
  },
  parseParam: function(to) {
    // console.log('to', to)
    this.reset()
    if (to instanceof Object) {
      this.to = this.pages.find(item => {
        if ((to.path && item.path === to.path) || (to.name && item.name === to.name)) {
          return true
        }
      })
      this.param = to.param
      this.query = to.query
    } else if (typeof to === 'string') {
      this.to = this.pages.find(item => {
        if (item.path === to || item.name === to) {
          return true
        }
      })
    } else {
      return
    }
    // console.log('this.to', this.to)

  },
  replace: function(to, onComplete, onAbort) {
    this.parseParam(to)
    if (this.beforeEachFun) {
      this.beforeEachFun(this.to, this.from, this.redirect)
      return
    }
    this.redirect(onComplete, onAbort)
  },
  redirect: function(to, onComplete, onAbort) {
    if (to) {
      this.parseParam(to)
    }
    let queryList = []
    if (!isNull(router.query)) {
      Object.keys(router.query).forEach(key => {
        queryList.push(key + '=' + router.query[key])
      })
    }
    let queryString = queryList.length !== 0 ? '?' + queryList.join('&') : ''
    // console.log('queryString', queryString)
    Taro.redirectTo({url: '/' + router.to.path + queryString, success: onComplete, fail: onAbort})
    if (router.afterEachFun) {
      router.afterEachFun(router.to, router.from)
    }
    router.from = router.to
  },
  push: function (to, onComplete, onAbort) {
    this.parseParam(to)
    if (this.beforeEachFun) {
      this.beforeEachFun(this.to, this.from, this.next)
      return
    }
    // uni.navigateTo({url: "/" + this.to.path})
    this.next(onComplete, onAbort)

  },
  next: function (to, onComplete, onAbort) {
    if (to) {
      this.parseParam(to)
    }
    let queryList = []
    if (!isNull(router.query)) {
      Object.keys(router.query).forEach(key => {
        queryList.push(key + '=' + router.query[key])
      })
    }
    let queryString = queryList.length !== 0 ? '?' + queryList.join('&') : ''
    // console.log('queryString', queryString)
    Taro.navigateTo({url: '/' + router.to.path + queryString, success: onComplete, fail: onAbort})
    if (router.afterEachFun) {
      router.afterEachFun(router.to, router.from)
    }
    router.from = router.to

  },
  go: function (delta) {
    Taro.navigateBack({delta: delta})
  }
}
router.init()
export default router
